<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>
    <link rel="stylesheet" href="../static/assets/global/plugins/bootstrap/css/bootstrap.min.css" type="text/css" />
    <link rel="stylesheet" href="../static/assets/global/css/components.css" type="text/css" />
    <link rel="stylesheet" href="../static/admin/pages/css/login.css" type="text/css" />
    <script src="../static/assets/global/plugins/jquery-1.11.0.min.js" type="text/javascript"></script>
</head>
<body class="login">
    <div class="content">
        <h3 class="form-title">商品详情</h3>
        <div class="form-group">
            <div>
                <label class="control-label"  id="title" name="title"/>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">商品描述</label>
            <div>
                <label class="control-label"  id="description" name="description"/>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">价格</label>
            <div>
                <label class="control-label"  id="price" name="price"/>
            </div>
        </div>
        <div class="form-group">
            <div>
                <img style="width: 200px;height: auto" id="imgUrl" name="imgUrl" />
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">库存</label>
            <div>
                <label class="control-label"  id="stock" name="stock"/>
            </div>
        </div>
        <div class="form-group">
            <label class="control-label">销量</label>
            <div>
                <label class="control-label"  id="sales" name="sales"/>
            </div>
        </div>
        <div class="form-actions">
            <button class="btn blue" id="createorder" type="submit">
                下单
            </button>
        </div>
    </div>
</body>
<script>
    function getParam(paramName) {
        paramValue = "",
            isFound = !1;
        if (this.location.search.indexOf("?") == 0 && this.location.search.indexOf("=") > 1) {
            arrSource = unescape(this.location.search).substring(1, this.location.search.length).split("&"),
                i = 0;
            while (i < arrSource.length && !isFound) arrSource[i].indexOf("=") > 0 && arrSource[i].split("=")[0].toLowerCase() == paramName.toLowerCase() && (paramValue = arrSource[i].split("=")[1], isFound = !0),
                i++
        }
        return paramValue == "" && (paramValue = null),
            paramValue
    }
    var g_itemVO = {};
    jQuery(document).ready(function () {
        //获取商品详情
        $.ajax({
            type:"GET",
            url:"http://localhost:8090/item/get",
            data:{
                "id":getParam("id")
            },
            xhrFields:{withCredentials:true},
            success:function(data){
                if(data.status=="success"){
                    g_itemVO = data.data;
                    reloadDom();
                }else{
                    alert("获取信息失败,原因为"+data.data.errMsg);
                }
            },
            error:function(data){
                alert("获取信息失败,原因为"+data.responseText);
            }
        });

        $("#createorder").on("click",function(){
            $.ajax({
                type:"POST",
                contentType:"application/x-www-form-urlencoded",
                url:"http://localhost:8090/order/createorder",
                data:{
                    "itemId":g_itemVO.id,
                    "amount":1
                },
                xhrFields:{withCredentials:true},
                success:function(data){
                    if(data.status=="success"){
                        alert("下单成功");
                        window.location.reload();
                    }else{
                        alert("下单失败,原因为"+data.data.errMsg);
                        if(data.data.errCode==20003){
                            window.location.href = "login.html";
                        }
                    }
                },
                error:function(data){
                    alert("下单失败,原因为"+data.responseText);
                }
            });
            return false;
        })
    })
    function reloadDom(){
        $("#title").text(g_itemVO.title);
        $("#description").text(g_itemVO.description);
        $("#price").text(g_itemVO.price);
        $("#imgUrl").attr("src",g_itemVO.imgUrl);
        $("#stock").text(g_itemVO.stock);
        $("#sales").text(g_itemVO.sales);
    }
</script>
</html>